package ydzhao.weixin.tuisong.util;

import com.alibaba.fastjson.JSONObject;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import ydzhao.weixin.tuisong.common.UserCom;

import java.util.Map;

/**
 * @author chlong
 * @date 2022/8/10
 */

public class PushUpdate {

    private static Map<String,String> updateMap = GuessMi.getGuessMi();

    /**
     * 推送更新
     * @param openId
     */
    public static void pushUpdate(String openId){
        //1，配置
        WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
        wxStorage.setAppId(UserCom.APPID);
        wxStorage.setSecret(UserCom.SECRET);
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxStorage);
        //模版id
        String templateId = "ljEQrLRY94Oyhnx6MkacYWOpBai0L00VRHKJHb55OUA";
        //2,推送消息
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser(openId)
                .templateId(templateId)
                .build();
        //3,如果是正式版发送模版消息，这里需要配置你的信息
//        templateMessage.addData(new WxMpTemplateData("name", "value", "#FF00FF"));
//        templateMessage.addData(new WxMpTemplateData(name2, value2, color2));
        //填写变量信息，比如天气之类的
        Map<String, Object> today = Tianqi.getNanjiTianqi(openId);
        JSONObject value = (JSONObject) today.get("today");
        JSONObject location = (JSONObject) today.get("location");

        templateMessage.addData(new WxMpTemplateData("riqi", value.get("date")+ "  "+ value.get("week"),"#00BFFF"));
        templateMessage.addData(new WxMpTemplateData("ip",location.get("city").toString(),"#145DBF"));
        templateMessage.addData(new WxMpTemplateData("tianqi",value.get("text_day").toString(),"#00FFFF"));
        templateMessage.addData(new WxMpTemplateData("low",value.get("low") + "","#173177"));
        templateMessage.addData(new WxMpTemplateData("high",value.get("high")+ "","#173177" ));
        templateMessage.addData(new WxMpTemplateData("caihongpi",updateMap.get("content"),"#FF69B4"));
        templateMessage.addData(new WxMpTemplateData("lianai",JiNianRi.getLianAi()+"","#7BE1E1"));
        templateMessage.addData(new WxMpTemplateData("shengri",JiNianRi.getShengRi()+"","#0969DA"));
        templateMessage.addData(new WxMpTemplateData("jinju", GuessMi.getJinJu()+"","#C71585"));
        templateMessage.addData(new WxMpTemplateData("tips","检测手动更新，答案将在2分钟后推送","#FF69B4"));
        String beizhu = "";
        if(JiNianRi.getLianAi() % 365 == 0){
            beizhu = "今天是在一起的周年纪念日！";
        }
        templateMessage.addData(new WxMpTemplateData("beizhu",beizhu,"#FF0000"));
        try {
            System.out.println(templateMessage.toJson());
            System.out.println(wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage));
        } catch (Exception e) {
            System.out.println("推送失败：" + e.getMessage());
            e.printStackTrace();
        }
    }

    /**
     * 答案更新推送
     * @param openId
     */
    public static void pushAnswerUpdate(String openId){
        //1，配置
        WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
        wxStorage.setAppId(UserCom.APPID);
        wxStorage.setSecret(UserCom.SECRET);
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxStorage);
        //模版id
        String templateId = "pXPHHYoa3REZyT6wY9Kh_LQHEwuxcab7kFldnhvLwdM";
        //2,推送消息
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser(openId)
                .templateId(templateId)
                .build();
        //3,如果是正式版发送模版消息，这里需要配置你的信息
//        templateMessage.addData(new WxMpTemplateData("name", "value", "#FF00FF"));
        templateMessage.addData(new WxMpTemplateData("caihongpi",updateMap.get("content"),"#FF69B4"));
        templateMessage.addData(new WxMpTemplateData("answer",updateMap.get("answer"),"#FF69B4"));
        templateMessage.addData(new WxMpTemplateData("reason",updateMap.get("reason"),"#FF69B4"));
        templateMessage.addData(new WxMpTemplateData("ps","宝~愿你每天好心情~","#FF1493"));

        try {
            System.out.println(templateMessage.toJson());
            System.out.println(wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage));
        } catch (Exception e) {
            System.out.println("推送失败：" + e.getMessage());
            e.printStackTrace();
        }finally {
            updateMap = GuessMi.getGuessMi();
        }
    }
}
